package com.PengGoBook.main.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;
import android.util.Log;

/**
 * Created with IntelliJ IDEA.
 * User: S7
 * Date: 13-5-28
 * Time: 上午1:34
 * To change this template use File | Settings | File Templates.
 */
public class DBHelper extends SQLiteOpenHelper {

    private static final String DB_NAME = "eoecn";
    private static final int DB_VERSION = 2;

    private SQLiteDatabase db;

    private static DBHelper mdbHelper;

    public static DBHelper getInstance(Context context)
    {
        if(mdbHelper==null)
        {
            Log.i("info_out","getInstance");
            mdbHelper=new DBHelper(context);
        }
        return mdbHelper;
    }

    private DBHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
    }

    private DBHelper(Context context, String name, SQLiteDatabase.CursorFactory factory,
                     int version) {
        super(context, name, factory, version);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        Log.i("info_out","db_onCreate");
        this.db = db;
        operateTable(db, "");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        if (oldVersion == newVersion) {
            return;
        }
        operateTable(db, "DROP TABLE IF EXISTS ");
        onCreate(db);
    }

    public void operateTable(SQLiteDatabase db, String actionString) {
        Log.i("info_out","operateTable");
        Class<DatabaseColumn>[] columnsClasses = DatabaseColumn.getSubClasses();
        DatabaseColumn columns = null;

        for (int i = 0; i < columnsClasses.length; i++) {
            try {
                columns = columnsClasses[i].newInstance();
                if ("".equals(actionString) || actionString == null) {
                    db.execSQL(columns.getTableCreateor());
                } else {
                    db.execSQL(actionString + columns.getTableName());
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

    public long insert(String Table_Name, ContentValues values) {
        if (db == null)
            db = getWritableDatabase();
        return db.insert(Table_Name, null, values);
    }

    /**
     *
     * @param Table_Name
     * @param id
     * @return 影响行数
     */
    public int delete(String Table_Name, int id) {
        if (db == null)
            db = getWritableDatabase();
        return db.delete(Table_Name, BaseColumns._ID + "=?",
                new String[] { String.valueOf(id) });
    }

    /**
     * @param Table_Name
     * @param values
     * @param WhereClause
     * @param whereArgs
     * @return 影响行数
     */
    public int update(String Table_Name, ContentValues values,
                      String WhereClause, String[] whereArgs) {
        if (db == null) {
            db = getWritableDatabase();
        }
        return db.update(Table_Name, values, WhereClause, whereArgs);
    }

    public Cursor query(String Table_Name, String[] columns, String whereStr,
                        String[] whereArgs) {
        if (db == null) {
            db = getReadableDatabase();
        }
        return db.query(Table_Name, columns, whereStr, whereArgs, null, null,
                null);
    }

    public Cursor rawQuery(String sql, String[] args) {
        if (db == null) {
            db = getReadableDatabase();
        }
        Log.i("info_out","sql:"+sql);
        return db.rawQuery(sql, args);
    }

    public void ExecSQL(String sql) {
        if (db == null) {
            db = getWritableDatabase();
        }
        db.execSQL(sql);
    }

    public void closeDb() {
        if (db != null) {
            db.close();
            db = null;
        }
    }

}
